﻿/**
 * @author sminutoli
 */
import com.dinmax.flash.draw.cabeza.*
import com.dinmax.flash.central.bridge.*
import com.dinmax.flash.central.extension.user.*
import com.dinmax.flash.central.extension.shortcut.*

class DATARootBridge
extends ControlCabeza
implements SelectedListener {
	
	public static var WRITE_MESSAGE:Number = 0;
	public static var MAKE_CALL:Number = 1;
	public static var FIND_PERSON:Number = 2;
	public static var SHORTCUTS:Number = 3;
	public static var FIND_PERSON_WAIT:String = "find";
	public static var FIND_PERSON_RESPONSE:String = "findResponse";
	
	private var type:Number;
	private var mcClose:ButtonCabeza;
			
	//write
	private var mcMessageSender:MESSAGESenderBridge;
	private var writeMessageTo:Number;
	
	//make
	private var mcMakeCall:MAKECALLBridge;
	
	//find
	private var mcFindPersonForm:FINDPERSONFormBridge;
	private var txt_response:TextField;
	private var mcResponseWindow:WINDOWResponseFinderBridge;
	private var mcWaitClip:MovieClip;
	private var response:Array;
	//
	
	//shortcuts
	private var shortcuts:Array;
	private var txt_shortcuts:TextField;
	private var mcShortcutsWindow:WINDOWContactBridge;
	//
	
	public function initChild(control:ControlCabeza):Void {
		control.addListener(this);
		switch(control){
					
			case mcResponseWindow:
			//trace("DATARootBridge.as initChild mcResponseWindow");
			this.mcResponseWindow.init();
			this.mcResponseWindow.deleteItems();
			this.displayResponse();
			break;
			
			case mcShortcutsWindow:
			this.mcShortcutsWindow.init();
			this.mcShortcutsWindow.deleteItems();
			this.displayShortcuts();
			break;
		};
		return;
	};
	
	public function display(type:Number):Void{
		this.setType(type);
		this.gotoAndStop(2);
		return;
	};
	
	private function endInterpolation():Void {
		switch(this.getType()){
			case DATARootBridge.WRITE_MESSAGE:
			//trace("DATARootBridge.as endInterpolation() write");
			this.gotoAndStop("write");
			var root:DATARootBridge = this;
			mcMessageSender.onLoad = function():Void {
				this.setListener(root);
				this.setTo(root.writeMessageTo);
				return;
			};
			break;
			
			case DATARootBridge.MAKE_CALL:
			//trace("DATARootBridge.as endInterpolation() make");
			this.gotoAndStop("make");
			break;
			
			case DATARootBridge.FIND_PERSON:
			//trace("DATARootBridge.as endInterpolation() find");
			this.gotoAndStop("find");
			this.mcWaitClip._visible=false;
			break;
			
			case DATARootBridge.SHORTCUTS:
			this.gotoAndStop("shortcuts");
			break;
		};
		return;
	};
	
	public function getPersonsResponse(response:Array):Void {
		//trace("DATARootBridge.as getPersonsResponse()");
		this.response = response;
		this.gotoAndStop(DATARootBridge.FIND_PERSON_RESPONSE);	
		return;
	};
	
	public function displayResponse():Void {
		//trace("DATARootBridge.as | displayResponse()");
		this.txt_response.html= true;
		this.txt_response.htmlText= "Coincidencias encontradas: <b>"+this.response.length+"</b>";
		for(var i:Number=0; i < this.response.length; i++){
			this.addPerson(Person(this.response[i]));
		};
		return;
	};
	
	public function addPerson(person:Person):Void{
		//trace("DATARootBridge.as addPerson()");
		this.mcResponseWindow.setLayout(new WindowCabezaLayout(mcResponseWindow));
		var usr:User = User(person);
		var isUser:Boolean = usr != null;
		var clip:MovieClip = this.mcResponseWindow.drawItem(person.getPhone(), person.getName()+" "+person.getLastName(), person.getEmail(), isUser);
		clip.setValue(person);
		clip.addListener(this);
		return;
	};
	
	public function setShortCuts(shortcuts:Array):Void {
		this.shortcuts=shortcuts;
		return;
	};
	
	public function displayShortcuts():Void {
		this.txt_shortcuts.html= true;
		this.txt_shortcuts.htmlText= "Atajos disponibles: <b>"+this.shortcuts.length+"</b>";
		for(var i:Number=0; i < this.shortcuts.length; i++){
			this.addShortcut(Shortcut(this.shortcuts[i]));
		};
		return;
	};
	
	public function addShortcut(shortcut:Shortcut):Void {
		this.mcShortcutsWindow.setLayout(new WindowCabezaLayout(mcShortcutsWindow));
		var clip:MovieClip = this.mcShortcutsWindow.drawItem(shortcut.getPhonenumber(), shortcut.getName(), shortcut.getEmail());
		clip.setValue(shortcut);
		clip.addListener(this);
		return;
	};
		
	public function setWriteMessageID(to:Number):Void{
		this.writeMessageTo = to;
		return;
	};	
	
	public function sendMessage(to:Number, subject:String, body:String):Void {
		CENTRALBridge.getActive().sendMessage(to, subject, body);
		return;
	};
	
	public function closeWindow():Void {
		this.gotoAndStop(1);
		return;
	};
	
	public function selected(control:ControlCabeza):Void {
		switch(control){
			case this.mcClose:
			this.closeWindow();
			break;
						
			case this.mcFindPersonForm:
			this.gotoAndStop(DATARootBridge.FIND_PERSON_WAIT);
			this.mcWaitClip._visible=true;
			break;
			
			case this.mcMakeCall:
			this.closeWindow();
			break;
						
		};
		return;
	};
	
	

	public function getType():Number {
		return type;
	}

	public function setType(type:Number):Void {
		this.type = type;
		return;
	}

}